package com.canaan.business.service.storehouse.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.canaan.business.common.enums.basic.YNEnum;
import com.canaan.business.common.enums.storehouse.CheckStatusEnum;
import com.canaan.business.domain.storehouse.*;
import com.canaan.business.domain.vo.StockDetailResultVo;
import com.canaan.business.service.storehouse.*;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.storehouse.StockCheckDetailMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 库存盘点任务明细Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-14
 */
@Service
public class StockCheckDetailServiceImpl implements IStockCheckDetailService {
    @Resource
    private StockCheckDetailMapper stockCheckDetailMapper;
    @Resource
    private IStockCheckResultService stockCheckResultService;
    @Resource
    private IWmsStorehouseStockService storehouseStockService;
    @Resource
    private IStockCheckService stockCheckService;
    @Resource
    private IStockCheckDiffService stockCheckDiffService;

    /**
     * 查询库存盘点任务明细
     *
     * @param id 库存盘点任务明细主键
     * @return 库存盘点任务明细
     */
    @Override
    public StockCheckDetail selectStockCheckDetailById(Long id) {
        return stockCheckDetailMapper.selectStockCheckDetailById(id);
    }

    /**
     * 查询库存盘点任务明细列表
     *
     * @param stockCheckDetail 库存盘点任务明细
     * @return 库存盘点任务明细
     */
    @Override
    public List<StockCheckDetail> selectStockCheckDetailList(StockCheckDetail stockCheckDetail) {
        return stockCheckDetailMapper.selectStockCheckDetailList(stockCheckDetail);
    }

    /**
     * 新增库存盘点任务明细
     *
     * @param stockCheckDetail 库存盘点任务明细
     * @return 结果
     */
    @Override
    public int insertStockCheckDetail(StockCheckDetail stockCheckDetail) {
        if(StringUtils.isBlank(stockCheckDetail.getNo())){
            stockCheckDetail.setNo(TableNoUtil.getNo(StockCheckDetail.class));
        }
        stockCheckDetail.setCreateBy(SecurityUtils.getUsername());
        stockCheckDetail.setCreateTime(DateUtils.getNowDate());
        return stockCheckDetailMapper.insertStockCheckDetail(stockCheckDetail);
    }

    /**
     * 批量新增库存盘点任务明细
     *
     * @param stockCheckDetailList 库存盘点任务明细列表
     * @return 结果
     */
    @Override
    public int batchInsertStockCheckDetail(List<StockCheckDetail> stockCheckDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StockCheckDetail stockCheckDetail : stockCheckDetailList){
            if(StringUtils.isBlank(stockCheckDetail.getNo())){
                stockCheckDetail.setNo(TableNoUtil.getNo(StockCheckDetail.class));
            }
            stockCheckDetail.setCreateBy(username);
            stockCheckDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<stockCheckDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > stockCheckDetailList.size()) {
                endIndex = stockCheckDetailList.size();
            }
            insertNum = insertNum + stockCheckDetailMapper.batchInsertStockCheckDetail(stockCheckDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改库存盘点任务明细
     *
     * @param stockCheckDetail 库存盘点任务明细
     * @return 结果
     */
    @Override
    public int updateStockCheckDetail(StockCheckDetail stockCheckDetail) {
        stockCheckDetail.setUpdateBy(SecurityUtils.getUsername());
        stockCheckDetail.setUpdateTime(DateUtils.getNowDate());
        return stockCheckDetailMapper.updateStockCheckDetail(stockCheckDetail);
    }

    /**
     * 批量修改库存盘点任务明细
     *
     * @param stockCheckDetailList 库存盘点任务明细列表
     * @return 结果
     */
    @Override
    public int batchUpdateStockCheckDetail(List<StockCheckDetail> stockCheckDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StockCheckDetail stockCheckDetail : stockCheckDetailList){
            stockCheckDetail.setUpdateBy(username);
            stockCheckDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<stockCheckDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > stockCheckDetailList.size()) {
                endIndex = stockCheckDetailList.size();
            }
            updateNum = updateNum + stockCheckDetailMapper.batchUpdateStockCheckDetail(stockCheckDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除库存盘点任务明细
     *
     * @param ids 需要删除的库存盘点任务明细主键集合
     * @return 结果
     */
    @Override
    public int deleteStockCheckDetailByIds(Long[] ids) {
        return stockCheckDetailMapper.deleteStockCheckDetailByIds(ids);
    }

    /**
     * 删除库存盘点任务明细信息
     *
     * @param id 库存盘点任务明细主键
     * @return 结果
     */
    @Override
    public int deleteStockCheckDetailById(Long id) {
        return stockCheckDetailMapper.deleteStockCheckDetailById(id);
    }

    /**
     * 查询库存盘点任务明细
     *
     * @param no 编号
     * @return 库存盘点任务明细
     */
    @Override
    public StockCheckDetail selectStockCheckDetailByNo(String no) {
        return stockCheckDetailMapper.selectStockCheckDetailByNo(no);
    }

    /**
     * 批量删除库存盘点任务明细
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteStockCheckDetailByNos(String[] nos) {
        return stockCheckDetailMapper.deleteStockCheckDetailByNos(nos);
    }

    /**
     * 删除库存盘点任务明细信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteStockCheckDetailByNo(String no) {
        return stockCheckDetailMapper.deleteStockCheckDetailByNo(no);
    }

    @Override
    public List<StockCheckDetail> selectStockCheckDetailByCheckNo(String checkNo) {
        StockCheckDetail detail = new StockCheckDetail();
        detail.setCheckNo(checkNo);
        return this.selectStockCheckDetailList(detail);
    }

    @Override
    public StockDetailResultVo getResult(Long id) {
        StockCheckDetail stockCheckDetail = this.selectStockCheckDetailById(id);
        StockDetailResultVo stockDetailResultVo = BeanUtil.toBean(stockCheckDetail, StockDetailResultVo.class);
        List<StockCheckResult> stockCheckResults = stockCheckResultService.selectStockCheckResultByCheckDetailNo(stockDetailResultVo.getNo());
        stockDetailResultVo.setStockCheckResultList(stockCheckResults);
        return stockDetailResultVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int finish(Long id) {
        StockCheckDetail stockCheckDetail = this.selectStockCheckDetailById(id);
        stockCheckDetail.setIsCheck(YNEnum.Y.getCode());
        this.updateStockCheckDetail(stockCheckDetail);
        List<StockCheckDetail> stockCheckDetails = this.selectStockCheckDetailByCheckNo(stockCheckDetail.getCheckNo());
        long count = stockCheckDetails.stream().filter(e -> YNEnum.Y.getCode().equals(e.getIsCheck())).count();
        //如果所有都已盘点
        if (count == stockCheckDetails.size() ) {
            //更新盘点任务状态
            StockCheck stockCheck = this.stockCheckService.selectStockCheckByNo(stockCheckDetail.getCheckNo());
            stockCheck.setStatus(CheckStatusEnum.COMPLETE.getCode());
            stockCheck.setCompleteTime(new Date());
            this.stockCheckService.updateStockCheck(stockCheck);
            List<StockCheckDiff> diffs = new ArrayList<>();
            for (StockCheckDetail detail: stockCheckDetails ){
                List<StockCheckResult> stockCheckResults = this.stockCheckResultService.selectStockCheckResultByCheckDetailNo(detail.getNo());
                for (StockCheckResult stockCheckResult : stockCheckResults) {
                    WmsStorehouseStock storehouseStock = this.storehouseStockService.selectWmsStorehouseStockListByBarcode(stockCheckResult.getBarcode());
                    StockCheckDiff diff = new StockCheckDiff();
                    diff.setCheckNo(stockCheckResult.getCheckNo());
                    diff.setCheckDetailNo(stockCheckResult.getCheckDetailNo());
                    diff.setProductNo(stockCheckResult.getProductNo());
                    diff.setProductName(stockCheckResult.getProductName());
                    diff.setProductUnit(stockCheckResult.getProductUnit());
                    diff.setStorehouseNo(stockCheckResult.getStorehouseNo());
                    diff.setAreaNo(stockCheckResult.getAreaNo());
                    diff.setLocationNo(stockCheckResult.getLocationNo());
                    diff.setBarcode(stockCheckResult.getBarcode());
                    diff.setStockQty(storehouseStock.getQty());
                    diff.setCheckQty(stockCheckResult.getQty());
                    diffs.add(diff);
                }
            }
            if (CollUtil.isNotEmpty(diffs)) {
               // 生成盘点差异
                this.stockCheckDiffService.batchInsertStockCheckDiff(diffs);
            }
        }
        return 1;
    }
}
